/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.jfonia.view.symbols;

import java.awt.Graphics2D;
import org.jfonia.assets.fonts.Fughetta;
import org.jfonia.assets.fonts.TextGlyph;
import org.jfonia.connect5.basics.ValueNode;
import org.jfonia.connect5.basics.Node;
import org.jfonia.connect5.basics.Observer;
import org.jfonia.constants.SymbolConstants;

/**
 *
 * @author Rik Bauwens
 */
public class FlagSymbol extends StaffSymbol
{
        protected TextGlyph textGlyph;

        public FlagSymbol(ValueNode<Double> frameX, ValueNode<Double> staffY,
                          ValueNode<Double> xNode, ValueNode<Double> yNode,
                          ValueNode<Integer> flagNode)
        {
                super(frameX, staffY, xNode, yNode);
                init(flagNode);
        }

        private void init(ValueNode<Integer> flagNode)
        {
                setFlag(flagNode.getValue());
                flagNode.addObserver(new Observer()
                {
                        public void onNotify(Object source)
                        {
                                setFlag(((ValueNode<Integer>) source).getValue());
                        }
                });
        }

        private void setFlag(int flagType)
        {
                if (flagType == SymbolConstants.FLAG_STEM_DOWN)
                        textGlyph = Fughetta.getGlyph(SymbolConstants.FLAG_STEM_DOWN_1_8);
                else if(flagType == SymbolConstants.FLAG_STEM_UP)
                        textGlyph = Fughetta.getGlyph(SymbolConstants.FLAG_STEM_UP_1_8);
                textGlyph.setSize(SymbolConstants.SYMBOL_SIZE);
                updateNodes();
        }

        protected void updateNodes()
        {
                preXNode.setValue(textGlyph.getBounds().getMinX());
                postXNode.setValue(textGlyph.getBounds().getMaxX());
                preYNode.setValue(- textGlyph.getBounds().getMinY());
                postYNode.setValue(textGlyph.getBounds().getMaxY());
        }

        @Override
        public void paintSymbol(Graphics2D g)
        {
                super.paintSymbol(g);
                textGlyph.paint(g, Math.ceil(getX()), Math.ceil(getY()));
        }
}
